/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package biofilter.util;

import biofilter.records.Record;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.lang.management.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * ProcessUtil provides functionality for:
 * 1. Unique naming of temp files in the filesystem
 * @author Daniel Quest
 * @date   Jan 6 2010
 */
public class ProcessUtil {

    public ProcessUtil(){

    }

    /**
     * getUniqueFileName takes an extension (e.g. .txt) and creates a fileName in the following
     * format:
     * PID_yyyy_MM_dd__HH_mm_ss.ext
     * So if the unix process id is: 37232
     * the time processing starts is 12 Hours, 01 Min, 34 sec
     * the date of processing is 2010/01/06 (Jan 6, 2010)
     * and the extension passed is .txt
     * This function will return the following:
     * 37232__2010_01_06__12_01_34.txt
     * @param ext
     * @return
     */
    public String getUniqueFileName(String ext) {
        try {
            return getProcessID() + "__" + getDateTime() + ext;
        } catch (Exception ex) {
            Logger.getLogger(ProcessUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * Assuming we are on a _NIX style system
     * @return
     */
    public String getProcessID() throws Exception {
        String id = "";
        String runName = ManagementFactory.getRuntimeMXBean().getName();
        String tokens[] = runName.split("@");
        if (tokens.length != 2) throw new Exception("getProcessID failed because process name could not be parsed! : " + runName);
        else id = tokens[0];
        return id;
    }

    public String getDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd__HH_mm_ss");
        Date date = new Date();
        return dateFormat.format(date);
    }

    public String getOutputDir(String path)throws FileNotFoundException{
        if (!path.endsWith("/")){
            path = path + "/";
        }
        File file=new File(path);
        if (!file.exists()){
            throw new FileNotFoundException("Path does not exist:" + path);
        }
        return path;
    }

    /**
     * input - a record
     * @return the name of the file where the file containing 1 record was created
     */
    public String recordToFile(Record r, String outputDir, String descriptor, String extention){
        String outfile = outputDir + getUniqueFileName( descriptor + extention);
          try {
              PrintWriter out = new PrintWriter(new FileWriter(outfile));
              out.println(r.get());
              out.close();
          } catch (IOException e){
              e.printStackTrace();
          }
        return outfile;
    }
}
